
import intranet.tools.WebConfig;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Properties;
import javax.mail.AuthenticationFailedException;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.*;
public class Sendmail extends HttpServlet{
private static final long serialVersionUID = 1L;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {
final String err = "/error.html";
PrintWriter out = response.getWriter();
//String id=request.getParameter("UserID");
String from = "NBC_Intranet_Mail_System";
String to = request.getParameter("NBCMail");
//String to ="da.born@nbc.org.kh";
String subject ="Activate your Account";
String str=request.getParameter("code");
//String str="dabornfdsfd";
WebConfig conf=new WebConfig();
String message ="Thanks your registering with our intranet!!\n To activate your account Please click on the link. http://172.16.5.66:9090/intranet/GetActivateCode.html?code="+str+"&mail="+to;
String login = "nbcintranet@nbc.org.kh";
String password ="123456";
//out.print("restult"+tos);
try {
Properties props = new Properties();
//props.setProperty("mail.host", "smtp.gmail.com");
props.setProperty("mail.host", "mail.nbc.org.kh");
props.setProperty("mail.smtp.port", "25");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.starttls.enable", "true");
Authenticator auth = new SMTPAuthenticator(login, password);
Session session = Session.getInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setText(message);
msg.setSubject(subject);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
Transport.send(msg);

response.sendRedirect("CompleteRegistration.html?email="+to+"");

} catch (AuthenticationFailedException ex) {
request.setAttribute("ErrorMessage", "Authentication failed");
RequestDispatcher dispatcher = request.getRequestDispatcher(err);
dispatcher.forward(request, response);
} catch (AddressException ex) {
request.setAttribute("ErrorMessage", "Wrong email address");
RequestDispatcher dispatcher = request.getRequestDispatcher(err);
dispatcher.forward(request, response);
} catch (MessagingException ex) {
request.setAttribute("ErrorMessage", ex.getMessage());
RequestDispatcher dispatcher = request.getRequestDispatcher(err);
dispatcher.forward(request, response); 
}
//RequestDispatcher dispatcher = request.getRequestDispatcher(succ);
//dispatcher.forward(request, response);
//out.println("<div align='center'><h2>Complete Registration</h2></div>");
//out.println("<div align='center'><h4>Your Activation code has been sent to your email :"+to+" . Please check your email and follow the instruction to activate your account.</br> Thanks</h4></div></br><div align='center'><a href=http://mail.nbc.org.kh>Activate Now...?</a></div>");
//out.println("<a target='_self'>");
}
private class SMTPAuthenticator extends Authenticator {

private PasswordAuthentication authentication;

public SMTPAuthenticator(String login, String password) {
authentication = new PasswordAuthentication(login, password);
}

protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}

protected void doGet(HttpServletRequest request, 
   HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

protected void doPost(HttpServletRequest request, 
    HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}